﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CubSettings : MonoBehaviour
{
        [Header("控制旋转的脚本")]
        public CubRotate cr;

        [Header("控制炸开的脚本")]
        public CubExplode ce;

        [Header("控制音乐的脚本")]
        public CubMusicCtrl mc;

        [Header("数值显示控件")]
        public GameObject objTxtValueShow;

        [Header("数值显示控件隐藏时间")]
        public int hideTime = 3;

        private CubSaveData sd;

        private bool isTxtValueShow;
        private Text txtValueShow;
        private bool isLoading;

        private const string OPEN = "开";
        private const string CLOSE = "关";
        private const string OBEY = "顺";
        private const string INVERSE = "逆";

        [Header("修改X轴设置的按钮")]
        public Button btnRotateX;
        /// <summary>
        /// 旋转设置X轴
        /// </summary>
        public void RotateSetX()
        {
                if (isLoading)
                {
                        return;
                }
                if (cr)
                {
                        cr.isRotateX = !cr.isRotateX;

                        string res;
                        if (cr.isRotateX)
                        {
                                res = OPEN;
                        }
                        else
                        {
                                res = CLOSE;
                        }
                        ShowValue("X轴旋转", res);

                        Text txtChange = btnRotateX.transform.Find("Text").GetComponent<Text>();

                        if (txtChange)
                        {
                                txtChange.text = res;
                        }
                }
        }
        [Header("修改Y轴设置的按钮")]
        public Button btnRotateY;
        /// <summary>
        /// 旋转设置Y轴
        /// </summary>
        public void RotateSetY()
        {
                if (isLoading)
                {
                        return;
                }
                if (cr)
                {
                        cr.isRotateY = !cr.isRotateY;

                        string res;
                        if (cr.isRotateY)
                        {
                                res = OPEN;
                        }
                        else
                        {
                                res = CLOSE;
                        }
                        ShowValue("Y轴旋转", res);

                        Text txtChange = btnRotateY.transform.Find("Text").GetComponent<Text>();

                        if (txtChange)
                        {
                                txtChange.text = res;
                        }
                }
        }
        [Header("修改Z轴设置的按钮")]
        public Button btnRotateZ;
        /// <summary>
        /// 旋转设置Z轴
        /// </summary>
        public void RotateSetZ()
        {
                if (isLoading)
                {
                        return;
                }
                if (cr)
                {
                        cr.isRotateZ = !cr.isRotateZ;

                        string res;
                        if (cr.isRotateZ)
                        {
                                res = OPEN;
                        }
                        else
                        {
                                res = CLOSE;
                        }
                        ShowValue("Z轴旋转", res);

                        Text txtChange = btnRotateZ.transform.Find("Text").GetComponent<Text>();

                        if (txtChange)
                        {
                                txtChange.text = res;
                        }
                }
        }
        [Header("修改旋转方向设置的按钮")]
        public Button btnRotateDir;
        /// <summary>
        /// 旋转设置方向
        /// </summary>
        public void RotateSetDir()
        {
                if (isLoading)
                {
                        return;
                }
                if (cr)
                {
                        cr.isClockWise = !cr.isClockWise;

                        string res;
                        if (cr.isClockWise)
                        {
                                res = OBEY;
                        }
                        else
                        {
                                res = INVERSE;
                        }
                        ShowValue("旋转方向", res);

                        Text txtChange = btnRotateDir.transform.Find("Text").GetComponent<Text>();

                        if (txtChange)
                        {
                                txtChange.text = res;
                        }
                }
        }
        [Header("旋转速度限度")]
        public float maxRotateSpeed = 117;
        public float minRotateSpeed = 0;
        [Header("修改旋转速度的进度条")]
        public Scrollbar scbRotateSpeed;
        /// <summary>
        /// 更新旋转速度
        /// </summary>
        public void RotateSetSpeed(float value)
        {
                if (isLoading)
                {
                        return;
                }
                if (cr)
                {
                        value = Mathf.Lerp(minRotateSpeed, maxRotateSpeed, value);
                        if (value >= minRotateSpeed && value <= maxRotateSpeed)
                        {
                                cr.speed = value;
                                ShowValue("旋转速度", cr.speed);
                        }
                }
        }
        [Header("炸开速度限度")]
        public float maxExplodeSpeed = 416;
        public float minExplodeSpeed = 17;
        [Header("修改炸开速度的进度条")]
        public Scrollbar scbExplodeSpeed;
        /// <summary>
        /// 更新炸开速度
        /// </summary>
        public void ExplodeSetSpeed(float value)
        {
                if (isLoading)
                {
                        return;
                }
                if (ce)
                {
                        value = Mathf.Lerp(minExplodeSpeed, maxExplodeSpeed, value);
                        if (value >= minExplodeSpeed && value <= maxExplodeSpeed)
                        {
                                ce.speed = value;
                                ShowValue("炸开速度", ce.speed);
                        }
                }
        }


        [Header("透明度限度")]
        public float maxColorValue = 255;
        public float minColorValue = 0;
        [Header("修改初始透明度的进度条")]
        public Scrollbar scbAlphaStart;
        /// <summary>
        /// 更新初始透明度
        /// </summary>
        public void AlphaSetStart(float value)
        {
                if (isLoading)
                {
                        return;
                }
                if (ce)
                {
                        value = Mathf.Lerp(minColorValue, maxColorValue, value);
                        if (value >= minColorValue && value <= maxColorValue)
                        {
                                ce.alphaCube = value;
                                ShowValue("默认透明度", ce.alphaCube);
                        }
                }
        }
        [Header("修改目标透明度的进度条")]
        public Scrollbar scbAlphaTag;
        /// <summary>
        /// 更新目标透明度
        /// </summary>
        public void AlphaSetTag(float value)
        {
                if (isLoading)
                {
                        return;
                }
                if (ce)
                {
                        value = Mathf.Lerp(minColorValue, maxColorValue, value);
                        if (value >= minColorValue && value <= maxColorValue)
                        {
                                ce.alphaTag = value;
                                ShowValue("炸开透明度", ce.alphaTag);
                        }
                }
        }

        [Header("缩放限度")]
        public float maxScaleValue = 5;
        public float minScaleValue = 0;
        [Header("修改初始缩放的进度条")]
        public Scrollbar scbScaleStart;
        /// <summary>
        /// 更新初始缩放
        /// </summary>
        public void ScaleSetStart(float value)
        {
                if (isLoading)
                {
                        return;
                }
                if (ce)
                {
                        value = Mathf.Lerp(minScaleValue, maxScaleValue, value);
                        if (value >= minScaleValue && value <= maxScaleValue)
                        {
                                ce.scaleCube = value;
                                ShowValue("默认缩放", ce.scaleCube);
                        }
                }
        }
        [Header("修改目标缩放的进度条")]
        public Scrollbar scbScaleTag;
        /// <summary>
        /// 更新目标缩放
        /// </summary>
        public void ScaleSetTag(float value)
        {
                if (isLoading)
                {
                        return;
                }
                if (ce)
                {
                        value = Mathf.Lerp(minScaleValue, maxScaleValue, value);
                        if (value >= minScaleValue && value <= maxScaleValue)
                        {
                                ce.scaleTag = value;
                                ShowValue("目标缩放", ce.scaleTag);
                        }
                }
        }

        [Header("音量调节进度条")]
        public Scrollbar scbMusicVol;
        /// <summary>
        /// 更新音量大小
        /// </summary>
        public void MusicSetVol(float value)
        {
                if (isLoading)
                {
                        return;
                }
                if (mc)
                {
                        value = value * 100;
                        if (value >= 0 && value <= 100)
                        {
                                mc.volMusic = value;
                                ShowValue("音乐音量", mc.volMusic);
                        }
                }
        }

        [Header("位移限度")]
        public float maxPositionValue = 50;
        public float minPositionValue = 0;
        [Header("修改初始位移的进度条")]
        public Scrollbar scbPositionStart;
        /// <summary>
        /// 更新初始位移
        /// </summary>
        public void PositionSetStart(float value)
        {
                if (isLoading)
                {
                        return;
                }
                if (ce)
                {
                        value = Mathf.Lerp(minPositionValue, maxPositionValue, value);
                        if (value >= minPositionValue && value <= maxPositionValue)
                        {
                                ce.positionCube = value;
                                ShowValue("默认位移", ce.positionCube);
                        }
                }
        }
        [Header("修改目标位移的进度条")]
        public Scrollbar scbPositionTag;
        /// <summary>
        /// 更新目标位移
        /// </summary>
        public void PositionSetTag(float value)
        {
                if (isLoading)
                {
                        return;
                }

                if (ce)
                {
                        value = Mathf.Lerp(minPositionValue, maxPositionValue, value);
                        if (value >= minPositionValue && value <= maxPositionValue)
                        {
                                ce.positionTag = value;
                                ShowValue("目标位移", ce.positionTag);
                        }
                }
        }
        [Header("显示关于文本的按钮")]
        public Button btnAbout;
        /// <summary>
        /// 点击关于
        /// </summary>
        public void TouchAbout()
        {
                Application.OpenURL(url);
        }
        [Header("音乐开关按钮")]
        public Button btnMusicSwitch;
        /// <summary>
        /// 音乐开关
        /// </summary>
        public void MusicSwitch()
        {
                if (isLoading)
                {
                        return;
                }
                if (mc)
                {
                        mc.enableMusic = !mc.enableMusic;

                        string res;
                        if (mc.enableMusic)
                        {
                                res = OPEN;
                        }
                        else
                        {
                                res = CLOSE;
                        }
                        ShowValue("音乐", res);

                        Text txtChange = btnMusicSwitch.transform.Find("Text").GetComponent<Text>();

                        if (txtChange)
                        {
                                txtChange.text = res;
                        }
                }
        }
        [Header("设置界面")]
        public GameObject[] SetInterfaces;
        private bool isShowSetting;
        /// <summary>
        /// 点击设置
        /// </summary>
        public void TouchSetting()
        {
                //修改界面可见
                isShowSetting = !isShowSetting;

                foreach (GameObject obj in SetInterfaces)
                {
                        obj.SetActive(isShowSetting);
                }

                if (isShowSetting)
                {
                        isLoading = true;
                        float value;
                        //初始化各种控件的值
                        if (cr)
                        {
                                if (scbRotateSpeed)
                                {
                                        value = (cr.speed - minRotateSpeed) / (maxRotateSpeed - minRotateSpeed);
                                        scbRotateSpeed.value = value;
                                }

                                if (btnRotateDir)
                                {
                                        Text txtChange = btnRotateDir.transform.Find("Text").GetComponent<Text>();
                                        if (cr.isClockWise)
                                        {
                                                txtChange.text = OBEY;
                                        }
                                        else
                                        {
                                                txtChange.text = INVERSE;
                                        }
                                }

                                if (btnRotateX)
                                {
                                        Text txtChange = btnRotateX.transform.Find("Text").GetComponent<Text>();
                                        if (cr.isRotateX)
                                        {
                                                txtChange.text = OPEN;
                                        }
                                        else
                                        {
                                                txtChange.text = CLOSE;
                                        }
                                }
                                if (btnRotateY)
                                {
                                        Text txtChange = btnRotateY.transform.Find("Text").GetComponent<Text>();
                                        if (cr.isRotateY)
                                        {
                                                txtChange.text = OPEN;
                                        }
                                        else
                                        {
                                                txtChange.text = CLOSE;
                                        }
                                }
                                if (btnRotateZ)
                                {
                                        Text txtChange = btnRotateZ.transform.Find("Text").GetComponent<Text>();
                                        if (cr.isRotateZ)
                                        {
                                                txtChange.text = OPEN;
                                        }
                                        else
                                        {
                                                txtChange.text = CLOSE;
                                        }
                                }
                        }

                        if (mc)
                        {
                                //弹出的声音
                                mc.SettingMusic();

                                if (scbMusicVol)
                                {
                                        value = (mc.volMusic/100);
                                        scbMusicVol.value = value;
                                }

                                if (btnMusicSwitch)
                                {
                                        Text txtChange = btnMusicSwitch.transform.Find("Text").GetComponent<Text>();
                                        if (mc.enableMusic)
                                        {
                                                txtChange.text = OPEN;
                                        }
                                        else
                                        {
                                                txtChange.text = CLOSE;
                                        }
                                }
                        }

                        if (ce)
                        {
                                if (scbExplodeSpeed)
                                {
                                        value = (ce.speed - minExplodeSpeed) / (maxExplodeSpeed - minExplodeSpeed);
                                        scbExplodeSpeed.value = value;
                                }

                                if (scbAlphaStart)
                                {
                                        value = (ce.alphaCube - minColorValue) / (maxColorValue - minColorValue);
                                        scbAlphaStart.value = value;
                                }

                                if (scbScaleStart)
                                {
                                        value = (ce.scaleCube - minScaleValue) / (maxScaleValue - minScaleValue);
                                        scbScaleStart.value = value;
                                }

                                if (scbPositionStart)
                                {
                                        value = (ce.positionCube - minPositionValue) / (maxPositionValue - minPositionValue);
                                        scbPositionStart.value = value;
                                }


                                if (scbAlphaTag)
                                {
                                        value = (ce.alphaTag - minColorValue) / (maxColorValue - minColorValue);
                                        scbAlphaTag.value = value;
                                }

                                if (scbScaleTag)
                                {
                                        value = (ce.scaleTag - minScaleValue) / (maxScaleValue - minScaleValue);
                                        scbScaleTag.value = value;
                                }

                                if (scbPositionTag)
                                {
                                        value = (ce.positionTag - minPositionValue) / (maxPositionValue - minPositionValue);
                                        scbPositionTag.value = value;
                                }
                        }
                        isLoading = false;
                }
        }
        void Save()
        {
                if (sd)
                {
                        sd.Save();
                }
        }
        void Start()
        {
                sd = GetComponent<CubSaveData>();
                isTxtValueShow = false;
                isShowSetting = false;
                //获取数值显示组件的初始化
                if (objTxtValueShow)
                {
                        txtValueShow = objTxtValueShow.GetComponent<Text>();
                        objTxtValueShow.SetActive(false);
                }
                //隐藏界面
                foreach (GameObject obj in SetInterfaces)
                {
                        obj.SetActive(isShowSetting);
                }
                //设置按钮监听
                if (btnAbout)
                {
                        btnAbout.onClick.AddListener(TouchAbout);
                }
                if (btnRotateX)
                {
                        btnRotateX.onClick.AddListener(RotateSetX);
                }
                if (btnRotateY)
                {
                        btnRotateY.onClick.AddListener(RotateSetY);
                }
                if (btnRotateZ)
                {
                        btnRotateZ.onClick.AddListener(RotateSetZ);
                }
                if (btnRotateDir)
                {
                        btnRotateDir.onClick.AddListener(RotateSetDir);
                }
                //设置拖动块监听
                //旋转速度
                if (scbRotateSpeed)
                {
                        scbRotateSpeed.onValueChanged.AddListener(RotateSetSpeed);
                }
                //炸开速度
                if (scbExplodeSpeed)
                {
                        scbExplodeSpeed.onValueChanged.AddListener(ExplodeSetSpeed);
                }
                //透明度
                if (scbAlphaStart)
                {
                        scbAlphaStart.onValueChanged.AddListener(AlphaSetStart);
                }
                if (scbAlphaTag)
                {
                        scbAlphaTag.onValueChanged.AddListener(AlphaSetTag);
                }
                //缩放
                if (scbScaleStart)
                {
                        scbScaleStart.onValueChanged.AddListener(ScaleSetStart);
                }
                if (scbScaleTag)
                {
                        scbScaleTag.onValueChanged.AddListener(ScaleSetTag);
                }
                //位移
                if (scbPositionStart)
                {
                        scbPositionStart.onValueChanged.AddListener(PositionSetStart);
                }
                if (scbPositionTag)
                {
                        scbPositionTag.onValueChanged.AddListener(PositionSetTag);
                }

                //音乐
                if (btnMusicSwitch)
                {
                        btnMusicSwitch.onClick.AddListener(MusicSwitch);
                }

                if (scbMusicVol)
                {
                        scbMusicVol.onValueChanged.AddListener(MusicSetVol);
                }
        }
        void Update()
        {
                HideTxtValueShow();
        }

        private long startTime;
        private bool isTestHideText;
        /// <summary>
        /// 负责数值组件的自动隐藏工作
        /// </summary>
        void HideTxtValueShow()
        {
                if (isTestHideText)
                {
                        Debug.Log("检测时间");
                        long nowTime = CubMinuyTool.ConvertDateTimeToLong(System.DateTime.Now);
                        if ((nowTime - startTime) > hideTime)
                        {
                                Debug.Log("超时");
                                isTestHideText = false;
                                objTxtValueShow.SetActive(false);
                        }
                }

                if (isTxtValueShow)
                {
                        Debug.Log("收到显示请求，更新时间戳！");
                        startTime = CubMinuyTool.ConvertDateTimeToLong(System.DateTime.Now);
                        isTestHideText = true;
                        isTxtValueShow = false;
                        objTxtValueShow.SetActive(true);
                }
        }
        /// <summary>
        /// 显示数值
        /// </summary>
        /// <param name="name">数值的名字</param>
        /// <param name="val">值</param>
        private void ShowValue(string name,float val)
        {
                ShowValue(name,val.ToString("0.00"));
        }
        /// <summary>
        /// 显示数值
        /// </summary>
        /// <param name="p">数值的名字</param>
        /// <param name="val">值</param>
        private void ShowValue(string p, string showStr)
        {

                isTxtValueShow = true;
                txtValueShow.text = p + "：" + showStr;
                Debug.Log(showStr);
                //修改之后保存一下
                this.Save();
        }
        /// <summary>
        /// 恢复默认设置
        /// </summary>
        public void SettingDefault()
        {
                TouchSetting();

                if (cr)
                {
                        cr.speed = CubSaveData.rotate_speed_default ;
                        cr.isClockWise = CubSaveData.rotate_isClockWise_default ;
                        cr.isRotateX = CubSaveData.rotate_isRotateX_default ;
                        cr.isRotateY = CubSaveData.rotate_isRotateY_default ;
                        cr.isRotateZ = CubSaveData.rotate_isRotateZ_default;
                }

                if (ce)
                {
                        ce.speed = CubSaveData.explode_speed_default;

                        ce.alphaCube = CubSaveData.explode_alphaCube_default;
                        ce.scaleCube = CubSaveData.explode_scaleCube_default;
                        ce.positionCube = CubSaveData.explode_positionCube_default;

                        ce.alphaTag = CubSaveData.explode_alphaTag_default ;
                        ce.scaleTag = CubSaveData.explode_scaleTag_default ;
                        ce.positionTag = CubSaveData.explode_positionTag_default;
                }

                if (mc)
                {
                        mc.volMusic = CubSaveData.music_volMusic_default;
                        mc.enableMusic = CubSaveData.music_enableMusic_default;
                }

                isTxtValueShow = true;
                txtValueShow.text = "恢复默认设置完成";
        }

        public void ExitApp()
        {
                Debug.Log("退出应用程序");
                Application.Quit();
        }

        public string url = "https://www.bilibili.com/video/bv19K411N7zr";
}
